<?php
namespace juven\libs {
    \defined( '_JEXEC' ) or die( 'Restricted access' );
    
    class ExceptionsManager {
        /** @var juven\libs\ExceptionsManager */
        protected static $_instance = \NULL;

        /** @var Boolean */
        public static $inDebugMode = \FALSE;

        public static function Init(){
            if( self::$_instance === \NULL ){ 
                self::$_instance = new self();
                \register_shutdown_function("juven\libs\ExceptionsManager::shutdownHandler");
                         \set_error_handler("juven\libs\ExceptionsManager::errorsHandler");
                     \set_exception_handler("juven\libs\ExceptionsManager::exceptionsHandler");
            }
            return self::$_instance;
        }

        public static function errorsHandler(){
            echo "\n<div class=\"server_error system_error\">\n";
            echo "<pre>\n";
            echo "Server Error:\n";
            echo "<br />\n";
            if (self::$inDebugMode==\TRUE){ //in test/debuging mode..
                \print_r(\func_get_args());
            } else { // in production...!!!
                echo "please try again later.";
            }
            echo "\n</pre>\n";
            echo "</div>\n";
            
            // todo: log error to the logging system
        }

        public static function exceptionsHandler(){
            echo "\n<div class=\"server_error system_error\">\n";
            echo "<pre>\n";
            echo "Server Exception:\n";
            echo "<br />\n";
            if (self::$inDebugMode==\TRUE){ //in test/debuging mode..
                \print_r(\func_get_args());
            } else { // in production...!!!
                echo "please try again later.";
            }
            echo "\n</pre>\n";
            echo "</div>\n";
            
            // todo: log Exception to the logging system
        }
        
        public static function shutdownHandler(){
            $e = @\error_get_last();
            if (@\is_array($e)) {
                $code = isset($e['type']) ? $e['type'] : 0;
                $msg = isset($e['message']) ? $e['message'] : 'undefined';
                $file = isset($e['file']) ? $e['file'] : 'undefined';
                $line = isset($e['line']) ? $e['line'] : 'undefined';
                
                echo "\n<div class=\"server_error system_error\">\n";
                echo "<pre>\n";
                echo "Application terminated unexpectedly:\n";
                echo "<br />\n";
                if (self::$inDebugMode==\TRUE){ //in test/debuging mode..
                    echo "File: $file<br />\n";
                    echo "Line: $line<br />\n";
                    echo "Msg : $msg<br />\n";
                    echo "Code: $code<br />\n";
                } else { // in production...!!!
                    echo "Server undefined state !, please try again later.";
                }
                
                echo "\n</pre>\n";
                echo "</div>\n";
                die();
//                \ob_start();
//                echo "<pre>";
//                \var_dump($e);
//                echo "</pre>";
//                $message = \ob_get_clean();
                //sendEmail($message);
//                \ob_start();
//                echo '{"status":"error","message":"Internal application error!"}';
//                \ob_flush();
//                exit();
                
//                if ($code>0) error_handler($code,$msg,$file,$line);
            }
        }
        protected function __construct(){}
        protected function __clone(){}
    }
    
    class juvenException extends \Exception {
        public function __construct($message='', $code=0, $previous=\NULL) {
            parent::__construct($message, $code, $previous);
            
        }
    }
    
}
?>
